let express = require('express');
let fs = require('fs');
let path = require('path');
let router = express.Router();
const {getAllHeatMap} = require("../util/business");

router.post('/heatMap/getAll', async (req, res, next) => {
    //异步调用
    getAllHeatMap().then((valueObj) => {
        res.send(valueObj);
    });
});


router.post('/heatMap/set', async (req, res, next) => {
    const reqBody = req.body;
    const { deviceMac, heatMapData } = reqBody;
    const filePath = path.join(__dirname, '../mock/data/heatMap.json');
    if (!deviceMac) {
        res.send({
            code: 200,
            msg: '请设置设备mac',
            data: null
        });
    }
    fs.readFile(
        filePath,
        'utf8',
        (err, data) => {
            const dataParse = (data && JSON.parse(data)) || {};
            dataParse[deviceMac] = typeof heatMapData === 'string' ? JSON.parse(heatMapData) : heatMapData;
            if (err) {
                logger.error(err.message || err.toString());
            }
            fs.writeFile(
                filePath,
                JSON.stringify(dataParse),
                'utf8',
                function (err) {
                    if (err) {
                        res.status(500);
                        res.send({
                            code: 500,
                            msg: '添加失败',
                            data: null
                        });
                    } else {
                        res.send({
                            code: 200,
                            msg: 'OK',
                            data: null
                        });
                    }
                }
            );
        });
});
router.post('/heatMap/get', async (req, res, next) => {
    const reqBody = req.body;
    const { deviceMac } = reqBody;
    const filePath = path.join(__dirname, '../mock/data/heatMap.json');
    if (!deviceMac) {
        res.send({
            code: 200,
            msg: '请设置设备mac',
            data: null
        });
        return;
    }
    fs.readFile(
        filePath,
        'utf8',
        (err, data) => {
            const dataParse = (data && JSON.parse(data)) || {};
            let resData = {
                [deviceMac]: dataParse[deviceMac] || null
            };
            if (err) {
                res.status(500);
                res.send({
                    code: 500,
                    msg: '暂无数据',
                    data: null
                });
                return;
            }
            res.send({
                code: 200,
                msg: 'OK',
                data: resData
            });
        });
});

router.post('/heatMap/getMore', async (req, res, next) => {
    const reqBody = req.body;
    const { deviceMacs } = reqBody;
    const filePath = path.join(__dirname, '../mock/data/heatMap.json');
    if (!deviceMacs) {
        res.send({
            code: 200,
            msg: '请设置设备mac',
            data: null
        });
        return;
    }
    fs.readFile(
        filePath,
        'utf8',
        (err, data) => {
            try {
                const dataParse = (data && JSON.parse(data)) || {};
                const deviceMacArr = JSON.parse(deviceMacs.replace(/'/g, '"') || null);
                let resData = null;
                if (deviceMacArr && Array.isArray(deviceMacArr)) {
                    resData = {};
                    deviceMacArr.forEach((deviceMac, index) => {
                        resData[deviceMac] = dataParse[deviceMac];
                    });
                } else {
                    res.send({
                        code: 200,
                        msg: '请设置设备mac',
                        data: null
                    });
                }
                if (err) {
                    throw err;
                } else {
                    res.send({
                        code: 200,
                        msg: 'OK',
                        data: resData
                    });
                }
            } catch (err) {
                res.status(500);
                res.send({
                    code: 500,
                    msg: err.message || err.toString(),
                    data: null
                });
            }
        });
});
router.post('/heatMap/setMore', async (req, res, next) => {
    const reqBody = req.body;
    const { deviceMacs, heatMapDatas } = reqBody;
    const filePath = path.join(__dirname, '../mock/data/heatMap.json');
    if (!deviceMacs) {
        res.send({
            code: 200,
            msg: '请设置设备mac',
            data: null
        });
        return;
    }
    fs.readFile(
        filePath,
        'utf8',
        (err, data) => {
            try {
                const dataParse = (data && JSON.parse(data)) || {};
                const deviceMacArr = JSON.parse(deviceMacs.replace(/'/g, '"') || null);
                const heatMapDatasArr = JSON.parse(heatMapDatas.replace(/'/g, '"') || null);
                if (deviceMacArr && Array.isArray(deviceMacArr)) {
                    deviceMacArr.forEach((deviceMac, index) => {
                        dataParse[deviceMac] =
                            typeof heatMapDatasArr[index] === 'string' ?
                                JSON.parse(heatMapDatasArr[index]) :
                                heatMapDatasArr[index];
                    });
                } else {
                    res.send({
                        code: 200,
                        msg: '请设置设备mac',
                        data: null
                    });
                }
                if (err) {
                    throw err;
                } else {
                    fs.writeFile(
                        filePath,
                        JSON.stringify(dataParse),
                        'utf8',
                        function (err) {
                            if (err) {
                                throw err;
                            } else {
                                res.send({
                                    code: 200,
                                    msg: 'OK',
                                    data: null
                                });
                            }
                        }
                    );
                }
            } catch (err) {
                res.status(500);
                res.send({
                    code: 500,
                    msg: err.message || err.toString(),
                    data: null
                });
            }
        });
});
router.post('/heatMap/clear', async (req, res, next) => {
    const filePath = path.join(__dirname, '../mock/data/heatMap.json');
    const reqBody = req.body;
    const { pwd } = reqBody;
    if (pwd !== 'lzj') {
        res.status(500);
        res.send({
            code: 500,
            msg: '暂无权限删除，请输入密码pwd',
            data: null
        });
        return;
    }
    fs.writeFile(
        filePath,
        JSON.stringify({}),
        'utf8',
        function (err) {
            if (err) {
                res.status(500);
                res.send({
                    code: 500,
                    msg: 'OK',
                    data: {}
                });
            } else {
                res.send({
                    code: 200,
                    msg: 'OK',
                    data: {}
                });
            }
        }
    );
});

module.exports = router;
